package gui.mainWindow;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JButton;

import questions.Questions;
import resource.Resource;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.Font;


@SuppressWarnings("serial")
public class MenuPanel extends JPanel {

	private JButton btnCaricaAll = new JButton("Carica tutte le domande");
	private JButton btnCaricaDim = new JButton("Carica tutte le domande con dimostrazione");
	private JButton btnEliminaProgressi = new JButton("Elimina progressi");
	private final JButton btnModificaRatio = new JButton("Modifica Ratio");
	
	private Questions questions;
	private MainWindow mainW;
	
	
	
	public MenuPanel(Questions q, MainWindow m) {
		questions = q;
		mainW = m;
		
		setLayout(new MigLayout("", "[grow][][grow]", "[grow][][][][][][][][grow]"));
		btnCaricaAll.setFont(new Font("DejaVu Sans", Font.BOLD, 14));
				
		btnCaricaAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Thread t = new Thread(new Runnable() {
					public void run() {
						try {
							questions.load();
							mainW.showQuestionsPanel();
						} catch (FileNotFoundException e) {
							JOptionPane.showMessageDialog(null, "Il file questions.txt non e' stato trovato", "Errore", JOptionPane.ERROR_MESSAGE, null);
							e.printStackTrace();
						} catch (IOException e) {
							JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Errore", JOptionPane.ERROR_MESSAGE, null);
							e.printStackTrace();
							System.exit(1);
						}
					}
				});
				t.start();
			}
		});
		
		
		add(btnCaricaAll, "cell 1 1,alignx center");
		btnCaricaDim.setFont(new Font("DejaVu Sans", Font.BOLD, 14));
		
		
		btnCaricaDim.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				Thread t = new Thread(new Runnable() {
					public void run() {
						//JOptionPane.showMessageDialog(null, "Funzione da implementare");
						try {
							questions.load();
							questions.selectOnlyDim();
							mainW.showQuestionsPanel();
						} catch (FileNotFoundException e) {
							JOptionPane.showMessageDialog(null, "Il file questions.txt non e' stato trovato", "Errore", JOptionPane.ERROR_MESSAGE, null);
							e.printStackTrace();
						} catch (IOException e) {
							JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Errore", JOptionPane.ERROR_MESSAGE, null);
							e.printStackTrace();
							System.exit(1);
						}
						
					}
				});
			t.start();
			}
		});
		add(btnCaricaDim, "cell 1 3,alignx center");
		btnEliminaProgressi.setFont(new Font("DejaVu Sans", Font.ITALIC, 12));
		
					
		btnEliminaProgressi.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				Thread t = new Thread(new Runnable() {
					public void run() {
						questions.deleteProgress();
						updateButton();
					}
				});
				t.start();
			}
		});
		
		add(btnEliminaProgressi, "cell 1 5,alignx center");
		
		btnModificaRatio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String input = JOptionPane.showInputDialog("inserisci un numero positivo, 0 disabilita il ratio", Resource.getRATIO());			
				try{
				Resource.setRATIO(Integer.parseInt(input));
				}catch(NumberFormatException n){
					JOptionPane.showMessageDialog(null, "input non valido", "ratio", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		add(btnModificaRatio, "cell 1 7,alignx center");

	}
	
	public void updateButton(){
		if(!questions.progressExists()){
			btnEliminaProgressi.setEnabled(false);
		}else{
			btnEliminaProgressi.setEnabled(true);
		}
	}
}
